package com.springcloud.controller;

import com.entities.Payment;
import com.entities.ResponseEntity;

import com.springcloud.Service.paymentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: springcloud
 * @description:
 * @author: zfy
 * @create: 2020-08-16 10:08
 */
@Controller
@ResponseBody
public class paymentConroller {

    @Resource
    @Autowired
//    获取注册信息
    private DiscoveryClient discoveryClient;
    @Autowired
    public paymentServiceImpl paymentServiceimpl;
    @GetMapping(value = "payment/select/{id}")
    public ResponseEntity selectByid(@PathVariable("id") String id){
        ResponseEntity r=new ResponseEntity();
       Payment i= paymentServiceimpl.selectByid(id);
        if (i!=null){
            r.setCode(200);
            r.setMessage("成功"+serverPort);
            r.setData(i);
        }else{
            r.setCode(500);
            r.setMessage("no成功"+serverPort);

        }
        return r;
    }
    @PostMapping("payment/insert")
    public ResponseEntity insert(String payment){
        ResponseEntity r=new ResponseEntity();
      int  i= paymentServiceimpl.Insert(payment);

        if (i!=0){
            r.setCode(200);
            r.setMessage("成功"+serverPort);
            r.setData(i);
        }else{
            r.setCode(500);
            r.setMessage("no成功"+serverPort);

        }
        return r;
    }
    @Value("${server.port}")
    private String serverPort;
    @GetMapping("discovery")
    public Object Getdiscovery(){
//        获取所有微服务
        List<String> services = discoveryClient.getServices();
        for (String s:services) {
            System.out.println(s);
        }
//        通过微服务名称获取所有内部id
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance s:instances) {
//                                服务名称       主机        端口        URL
            System.out.println(s.getServiceId()+s.getHost()+s.getPort()+s.getUri());
        }
        return discoveryClient;
    }

    @GetMapping("payment/lib")
    public String getServerPort(){
        return serverPort;
    }

}